//
//  ZZCEmoticonViewCell.swift
//  weibo
//
//  Created by zhangzuchao on 2017/4/6.
//  Copyright © 2017年 zhangZuChao. All rights reserved.
//

import UIKit

class ZZCEmoticonViewCell: UICollectionViewCell {
    
    //  删除按钮
    fileprivate lazy var deleteButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        return button
    }()
    
    //  表情按钮的数组
    fileprivate lazy var emoticonButtonArray: [ZZCEmoticonButton] = [ZZCEmoticonButton]()
    
    //  从外界拿到数据, 直接给本cell赋值
    var emoticonArray: [ZZCEmoticon]? {
        didSet {
            guard let currentEmoticonArray = emoticonArray else {
                return
            }
            //  默认表情全部隐藏
            for button in emoticonButtonArray {
                button.isHidden = true
            }
            
            
            //  遍历表情数组模型给表情按钮赋值
            for (i, emoticon) in currentEmoticonArray.enumerated() {
                //  根据下标获取表情按钮
                let button = emoticonButtonArray[i]
                 button.emoticon = emoticon
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        
        //  添加20个表情按钮
        addChildButton()
        
        //添加删除按钮
        contentView.addSubview(deleteButton)
      }
    
    //  添加20个表情按钮
    fileprivate func addChildButton() {
        //   需要在layoutsubView里面去子控件布局
        for _ in 0..<20 {
            let button = ZZCEmoticonButton()
            //  点击事件
            button.addTarget(self, action: #selector(emoticonButtonAction(btn:)), for: .touchUpInside)
            //  设置文字大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
//            button.backgroundColor = RandomColor()
            contentView.addSubview(button)
            //  保存表情按钮
            emoticonButtonArray.append(button)
        }
    }
    
    //  点击单个表情的事件
    @objc private func emoticonButtonAction(btn: ZZCEmoticonButton) {
        let emoticon = btn.emoticon!
        //  发送通知给textView设置值
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedEmoitconButtonNoti), object: emoticon)
    }
    
    //  点击删除按钮的事件
    @objc private func deleteButtonAction() {
        //  发送删除表情的通知
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedDeleteEmoticonNoti), object: nil)
    }

    //  完成子控件的布局
    override func layoutSubviews() {
        super.layoutSubviews()
        //  每项的宽度
        let buttonWidth = width / 7
        //  每项的高度
        let buttonHeight = height / 3
        
        for (i,button) in emoticonButtonArray.enumerated() {
            //  根据下标计算列索引
            let colIndex = i % 7
            //  根据下标计算行索引
            let rowIndex = i / 7
            //  设置x坐标
            button.x = CGFloat(colIndex) * buttonWidth
            //  设置y坐标
            button.y = CGFloat(rowIndex) * buttonHeight
            //  设置大小
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
        }
        
        //  设置删除按钮的大小
        deleteButton.size = CGSize(width: buttonWidth, height: buttonHeight)
        deleteButton.x = width - buttonWidth
        deleteButton.y = height - buttonHeight

        
    }

}
